package com.topvision.bureau.view.regmode;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;

import com.kernal.demo.plateid.R;
import com.topvision.bureau.model.regmode.RegMode;
import com.topvision.bureau.presenter.RegModePresenter;
import com.topvision.commomsdk.TouchInterface;
import com.topvision.commomsdk.activity.BaseFragmentActivity;
import com.topvision.commomsdk.constants.TvConstants;
import com.topvision.commomsdk.uitl.AndroidUtils;
import com.topvision.commomsdk.view.TvViewPager;
import com.topvision.idcard.CameraActivity;
import com.topvision.voicesdk.BaseVoiceFragmentActivity;
import com.topvision.voicesdk.CommonVoice;
import com.topvision.voicesdk.VoiceInterface;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 识别算法主类
 * User: jack(jackgu@topvision-cv.com)
 * Date: 2017-06-16
 * Time: 18:44
 */
public class RegModeActivity extends BaseVoiceFragmentActivity implements IRegModeView, TouchInterface, VoiceInterface {
    Context context;
    RegModePresenter regModePresenter;
    RegModeFragmentAdapter adapter;
    List<Fragment> regModeFragments;
    int currentPosition;
    Activity mActivity;



    @BindView(R.id.viewPager)
    TvViewPager viewPager;
    @BindView(R.id.allView)
    View allView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg_mode);
        context = this;
        mActivity = this;
        //绑定activity
        ButterKnife.bind(this);
        //初始化控制器
        regModePresenter = new RegModePresenter(context, this);
        initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setTouchInterfaceListener(this);
        setVoiceInterfaceListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        setTouchInterfaceListener(null);
        setVoiceInterfaceListener(null);
    }

    //初始化视图
    private void initData() {
        regModePresenter.getRegModes();
    }


    @Override
    public void showToast(String msg) {

    }

    @Override
    public void showRegModesView(List<RegMode> regModes) {
        regModeFragments = new ArrayList<>();
        if (regModes != null && regModes.size() > 0) {
            for (RegMode regMode : regModes) {
                RegModeFragment regModeFragment = RegModeFragment.newInstance(regMode);
                regModeFragments.add(regModeFragment);
            }
            adapter = new RegModeFragmentAdapter(getSupportFragmentManager(), regModeFragments);
            viewPager.setNoScroll(true);
            // 设置2张图之前的间距。
            viewPager.setPageMargin(200);
            //滑动重新绘制item
            viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
            if (AndroidUtils.isPad(context)) {
                RelativeLayout.LayoutParams paramTest = (RelativeLayout.LayoutParams) viewPager.getLayoutParams();
                paramTest.leftMargin = (int) getResources().getDimension(R.dimen.app_margin);
                paramTest.rightMargin = (int) getResources().getDimension(R.dimen.app_margin);
                viewPager.setLayoutParams(paramTest);
            }
            //设置viewpager滑动动画
            AndroidUtils.setAnimation(viewPager);
            viewPager.setAdapter(adapter);
        }
    }


    @Override
    public void doForwardTouch() {
        if (viewPager != null) {
            if (regModeFragments != null && regModeFragments.size() > 0) {
                if (currentPosition + 1 < regModeFragments.size()) {
                    currentPosition++;
                    viewPager.setCurrentItem(currentPosition);
                }
            }
        }
    }

    @Override
    public void doBackwardTouch() {
        if (viewPager != null) {
            if (regModeFragments != null && regModeFragments.size() > 0) {
                if (currentPosition > 0) {
                    currentPosition--;
                    viewPager.setCurrentItem(currentPosition);
                }
            }
        }
    }

    @Override
    public void doClickTouch() {
        if (regModeFragments != null && regModeFragments.size() > 0) {
            Fragment currentFragment = regModeFragments
                    .get(currentPosition);
            Bundle bundle = currentFragment.getArguments();
            final RegMode regMode = bundle
                    .getParcelable(RegModeFragment.REG_MODE);
            regModePresenter.forwardRegMode(mActivity, regMode);
        }
    }


    @Override
    public void doDoubleTouch() {

    }


    @Override
    public void dealWithVoice(String data) {
        if (data.contains(CommonVoice.VOICE_ZH_DOWN)) {
            doForwardTouch();
        } else if (data.contains(CommonVoice.VOICE_ZH_UP)) {
            doBackwardTouch();
        } else if (data.contains(CommonVoice.VOICE_ZH_BACK)) {
            finish();
        } else if (data.contains(getString(R.string.face_reg))) {
            regModePresenter.forwardChooseFaceMode();
        } else if (data.contains(getString(R.string.car_reg))) {
            regModePresenter.forwardChooseCarMode();
        } else if (data.contains(getString(R.string.bank_card_reg))) {
            regModePresenter.forwardBankCardMode(CameraActivity.TYPE_BANK_CAR);
        } else if (data.contains(getString(R.string.identity_reg))) {
            regModePresenter.forwardBankCardMode(CameraActivity.TYPE_ORC);
        } else if (data.contains(CommonVoice.VOICE_ZH_OK)) {
            doClickTouch();
        }
    }

    /**
     * 监听Viewpager翻页事件
     * create at 2016/12/16 16:41
     */
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrolled(int position, float positionOffset,
                                   int positionOffsetPixels) {
            // to refresh frameLayout
            if (allView != null) {
                allView.invalidate();
            }
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}